package com.das.mechanic_base.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.camera.TagLocation;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3SoundPlayUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;

import java.util.HashMap;

public class X3PictureNewTagLayout extends RelativeLayout implements View.OnTouchListener {

    /**
     * 存储内容+百分比坐标
     */
    private Multimap<String, TagLocation> hashMap = ArrayListMultimap.create();

    private static final int CLICKRANGE = 10;

    // 按下 和 抬起
    int startX, startY, endX, endY, lastX, lastY;

    /**
     * 正在触摸的view
     */
    private View touchView;

    /**
     * 测量文字宽度
     */
    private Paint textPaint;

    /**
     * 刚开始触摸时距离左 上的距离
     */
    int startTouchViewLeft = 0;
    int startTouchViewTop = 0;

    private Context mContext;
    private Rect textRect;

    private int viewWidth, viewHeight, viewLeft;
    private ImageView noticeView;

    public static int offSetX = 10;
    public static int offSetY = 10;

    private OnTouchLayoutListence onTouchLayoutListence;

    private LayoutParams deleteImageLayout;
    private LinearLayout ll_del;
    private View delView;

    private int maxHeight;

    private boolean isScaleIconTouch;

    public X3PictureNewTagLayout(Context context) {
        this(context, null);
    }

    public X3PictureNewTagLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public void addMaxHeight(int height) {
        this.maxHeight = height;
    }

    private void initView() {
        this.setOnTouchListener(this);
        this.setOnClickListener(view -> {
            if (onTouchLayoutListence != null) {
//                onTouchLayoutListence.OnTouchListener();
            }
        });

        startY = -1;
        startX = -1;
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(X3ScreenUtils.dipToPx(12, mContext));

        viewHeight = X3ScreenUtils.dipToPx(32, mContext);
        viewWidth = X3ScreenUtils.dipToPx(52, mContext);
        viewLeft = X3ScreenUtils.dipToPx(20, mContext);

        // 初始一个imageview 作为删除按钮
        delView = View.inflate(mContext, R.layout.x3_tag_del_bg, null);
        ll_del = delView.findViewById(R.id.ll_del);
        deleteImageLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        deleteImageLayout.width = X3ScreenUtils.getScreenWidth(mContext);
        deleteImageLayout.height = LayoutParams.WRAP_CONTENT;
        deleteImageLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        deleteImageLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);
        addView(delView, deleteImageLayout);
        delView.setAlpha(0);
    }

    private boolean hasView;

    private boolean isCanTouch = false;
    public static float SCALE_MAX = 35.5f; //最大的缩放比例
    private static float SCALE_MIN = 1.0f;
    private double oldDist = 0;
    private double moveDist = 0;
    private float downX1 = 0;
    private float downX2 = 0;
    private float downY1 = 0;
    private float downY2 = 0;

    private double preSpace = 0.0;

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int pointerCount = event.getPointerCount();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:

                /**
                 * 获取按下的起始坐标
                 */
                startX = (int) event.getX();
                startY = (int) event.getY();

                lastX = (int) event.getX();
                lastY = (int) event.getY();

                if (noticeView != null) {
                    removeView(noticeView);
                }
                //如果点击位置已经有了
                if (hasView(startX, startY)) {
                    hasView = true;
                    startTouchViewLeft = touchView.getLeft();
                    startTouchViewTop = touchView.getTop();
                    TagLocation tagLocation = (TagLocation) touchView.getTag();
                    String currentName = ((X3TagImageView) touchView).getShare();
                    currentScale = getScale(currentName);
                    SCALE_MAX = 35.5f;
                    getScaleValue();
                } else {
                    hasView = false;
                }
                break;
            case MotionEvent.ACTION_MOVE:

                if (isScaleIconTouch && !((X3TagImageView) touchView).isHindBitmap) {

                    VelocityTracker mVelocityTracker = VelocityTracker.obtain();
                    mVelocityTracker.addMovement(event);
                    mVelocityTracker.computeCurrentVelocity(1000);
                    float xVelocity = mVelocityTracker.getXVelocity();//速度向左滑，值为负数，向右滑为正

                    float x = (int) event.getX() - startX;
                    float y = (int) event.getY() - startY;
                    if (Math.abs(xVelocity) >= 8.5) {

                        int space = (int) Math.sqrt(x * x + y * y);

                        lastX = (int) event.getX();
                        lastY = (int) event.getY();
                        if (touchView != null) {
                            if (((X3TagImageView) touchView).direction == X3TagImageView.Direction.Left) {
                                if (xVelocity < 0) {
                                    space = -Math.abs(space);
                                } else {
                                    space = Math.abs(space);
                                }
                            } else {
                                if (xVelocity < 0) {
                                    space = Math.abs(space);
                                } else {
                                    space = -Math.abs(space);
                                }
                            }
                            if (Math.abs(space) >= 0.5) {
                                float scale = (float) (getTouchViewScaleX() + ((space) / (getWidth() * 4.5f)));
                                if (scale < SCALE_MIN) {
                                    setScale(SCALE_MIN);
                                } else if (scale > SCALE_MAX) {
                                    setScale(SCALE_MAX);
                                } else {
                                    setScale(scale);
                                }
                            }
                        }
                    }

                } else if (pointerCount == 2 && touchView != null) {                // 双指缩放

                    float x1 = event.getX(0);
                    float x2 = event.getX(1);
                    float y1 = event.getY(0);
                    float y2 = event.getY(1);

                    double changeX1 = x1 - downX1;
                    double changeX2 = x2 - downX2;
                    double changeY1 = y1 - downY1;
                    double changeY2 = y2 - downY2;

                    if (getScaleX() > 1) { //滑动
                        float lessX = (float) ((changeX1) / 2 + (changeX2) / 2);
                        float lessY = (float) ((changeY1) / 2 + (changeY2) / 2);
//                        setSelfPivot(-lessX, -lessY);
                    }
                    //缩放处理
                    moveDist = spacing(event);
                    double space = moveDist - oldDist;

                    if (Math.abs(space - preSpace) >= 0.5) {
                        preSpace = space;
                        float scale = (float) (getTouchViewScaleX() + ((space) / (getWidth() * 4.5f)));
                        if (scale < SCALE_MIN) {
                            setScale(SCALE_MIN);
                        } else if (scale > SCALE_MAX) {
                            setScale(SCALE_MAX);
                        } else {
                            setScale(scale);
                        }
                    }

                } else {  // 正常单指拖动

                    endX = (int) event.getX();
                    endY = (int) event.getY();

                    if ((Math.abs(event.getX() - startX) >= CLICKRANGE || Math.abs(event.getY() - startY) >= CLICKRANGE) && hasView) {
                        delView.setAlpha(1);
                        if (onTouchLayoutListence != null) {
                            onTouchLayoutListence.OnShowBottomView(false);
                        }
                        getParent().requestDisallowInterceptTouchEvent(true);
                        // 如何滑动的过程中 触摸点在删除的范围内 则将删除按钮放大
                        if (event.getY() >= delView.getTop()) {
                            ll_del.setBackgroundColor(Color.parseColor("#F64458"));
                        } else {
                            ll_del.setBackgroundColor(Color.parseColor("#292929"));
                        }

                        // 移动view
                        moveView((int) event.getX(), (int) event.getY());
                    }
                }

                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                if (pointerCount == 2) {
                    downX1 = event.getX(0);
                    downX2 = event.getX(1);
                    downY1 = event.getY(0);
                    downY2 = event.getY(1);
                    oldDist = spacing(event); //两点按下时的距离
                }
                break;

            case MotionEvent.ACTION_UP:
                isScaleIconTouch = false;
                changeOrientation = false;
                if (pointerCount == 2) {
                    downX1 = 0;
                    downY1 = 0;
                    downX2 = 0;
                    downY2 = 0;
                }

                hasView = false;
                endX = (int) event.getX();
                endY = (int) event.getY();

//                moveView(endX,endY);
                if (onTouchLayoutListence != null) {
                    onTouchLayoutListence.OnShowBottomView(true);
                }
                delView.animate().alpha(0).setDuration(100).start();

                //如果挪动的范围很小，则判定为单击 改变方向
                if (Math.abs(endX - startX) < CLICKRANGE && Math.abs(endY - startY) < CLICKRANGE) {
                    hindAllView();
                    ((X3TagImageView) touchView).setHindBitmap(!((X3TagImageView) touchView).isHindBitmap);
                    ((X3TagImageView) touchView).refreshTag();
                } else {
                    if (endY >= delView.getY()) {
                        if (touchView != null) {
                            X3SoundPlayUtils.play(2);
                            hashMap.remove(((X3TagImageView) touchView).getShare(), touchView.getTag());
                            removeView(touchView);
                            touchView = null;
                            if (onTouchLayoutListence != null) {
                                onTouchLayoutListence.OnChangeTagLocation();
                            }
                        }
                    } else {
                        if (touchView != null && touchView.getBottom() >= maxHeight) {
                            moveView(endX, maxHeight - viewHeight);
                        }
                    }
                }
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return false;
    }

    //切换控件方向
    private void switchOrientation(){
        int currentX = getWidth() / 2;
        if (touchView != null && hasView(endX, endY)) {
            Rect textRect1 = new Rect();
            String share = ((X3TagImageView) touchView).getShare();
            textPaint.getTextBounds(share, 0, share.length(), textRect1);

            int isLeft = 0;
            //如果点击位置已经有了
            if (X3TagImageView.Direction.Left.equals(((X3TagImageView) touchView).getDirection())) {

                LayoutParams layoutParams = (LayoutParams) touchView.getLayoutParams();
                layoutParams.leftMargin = layoutParams.leftMargin - layoutParams.width + viewLeft;

                if (layoutParams.leftMargin <= 0) {
                    layoutParams.leftMargin = 0;
                }
                touchView.setLayoutParams(layoutParams);
                ((X3TagImageView) touchView).changeTag(share, X3TagImageView.Direction.Right);
                isLeft = 2;

                currentX = layoutParams.leftMargin;

            } else if (X3TagImageView.Direction.Right.equals(((X3TagImageView) touchView).getDirection())) {

                LayoutParams layoutParams = (LayoutParams) touchView.getLayoutParams();
                layoutParams.leftMargin = layoutParams.leftMargin + layoutParams.width - viewLeft;
                if (layoutParams.leftMargin >= getWidth() - layoutParams.width) {
                    layoutParams.leftMargin = getWidth() - layoutParams.width;
                }
                touchView.setLayoutParams(layoutParams);
                ((X3TagImageView) touchView).changeTag(share, X3TagImageView.Direction.Left);
                isLeft = 1;
                currentX = layoutParams.leftMargin;
            }

            hashMap.remove(share, touchView.getTag());

            TagLocation tagLocation = new TagLocation(currentX * 1f / getWidth(), touchView.getTop() * 1f / getHeight(), isLeft);
            tagLocation.scale = currentScale;

            hashMap.put(share, tagLocation);
            touchView.setTag(tagLocation);

            if (onTouchLayoutListence != null) {
                onTouchLayoutListence.OnChangeTagLocation();
            }
        } else {
            moveView(endX, endY);
            if (onTouchLayoutListence != null) {
                onTouchLayoutListence.OnTouchListener();
            }
        }
    }

    /**
     * 触摸使用的移动事件
     *
     * @param lessX
     * @param lessY
     */
    private void setSelfPivot(float lessX, float lessY) {
        float setPivotX = 0;
        float setPivotY = 0;
        setPivotX = getPivotX() + lessX;
        setPivotY = getPivotY() + lessY;
        if (setPivotX < 0 && setPivotY < 0) {
            setPivotX = 0;
            setPivotY = 0;
        } else if (setPivotX > 0 && setPivotY < 0) {
            setPivotY = 0;
            if (setPivotX > getWidth()) {
                setPivotX = getWidth();
            }
        } else if (setPivotX < 0 && setPivotY > 0) {
            setPivotX = 0;
            if (setPivotY > getHeight()) {
                setPivotY = getHeight();
            }
        } else {
            if (setPivotX > getWidth()) {
                setPivotX = getWidth();
            }
            if (setPivotY > getHeight()) {
                setPivotY = getHeight();
            }
        }
        setPivot(setPivotX, setPivotY);
    }

    /**
     * 计算两个点的距离
     *
     * @param event
     * @return
     */
    private double spacing(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return Math.sqrt(x * x + y * y);
        } else {
            return 0;
        }
    }

    /**
     * 平移画面，当画面的宽或高大于屏幕宽高时，调用此方法进行平移
     *
     * @param x
     * @param y
     */
    public void setPivot(float x, float y) {
        if (touchView != null) {
            touchView.setPivotX(x);
            touchView.setPivotY(y);
        }
    }

    /**
     * 设置放大缩小
     *
     * @param scale
     */
    public void setScale(float scale) {

        if (touchView != null) {
            currentScale = scale;
            TagLocation tagLocation = (TagLocation) touchView.getTag();
            String currentName = ((X3TagImageView) touchView).getShare();
            hashMap.remove(currentName, tagLocation);
            putScale(currentName, scale);
            tagLocation.scale = scale;
            touchView.setTag(tagLocation);
            hashMap.put(currentName, tagLocation);

            ((X3TagImageView) touchView).refreshTag(scale, true);
            refreshLayoutParams(scale);
        }
    }

    HashMap<String, Float> scaleMap = new HashMap<>();

    public float getScale(String name) {
        float scale = 1.0f;
        if (scaleMap.containsKey(name)) {
            try {
                scale = scaleMap.get(name);
            } catch (Exception e) {
            }
        }
        if (scale <= 1.0) {
            return 1.0f;
        }
        return scale;
    }

    public void putScale(String name, float scale) {
        scaleMap.put(name, scale);
    }

    int originW = 1;
    HashMap<String, Integer> wMap = new HashMap<>();

    public int getW() {
        if (touchView != null) {
            String name = ((X3TagImageView) touchView).getShare();
            if (wMap.containsKey(name)) {
                originW = wMap.get(name);
            } else {
                wMap.put(name, touchView.getWidth());
                originW = touchView.getWidth();
            }
        }
        return originW;
    }

    public float getScaleValue() {
//        SCALE_MAX = getWidth() * 1.0f / getW();
        return SCALE_MAX;
    }

    float currentScale = 1.0f;

    /**
     * 设置放大缩小
     */
    public float getTouchViewScaleX() {
//        int sx = 1;
//        if (touchView != null){
//            sx = (int) touchView.getScaleX();
//        }
        return currentScale;
    }

    /**
     * 初始化比例，也就是原始比例
     */
    public void setInitScale() {
        setScaleX(1.0f);
        setScaleY(1.0f);
        setPivot(getWidth() / 2, getHeight() / 2);
    }

    public void addData(String name, boolean isFlag, int isLeft) {
        addData(name, isFlag, isLeft, 1.0f);
    }

    public void addData(String name, boolean isFlag, int isLeft, float hasSetScale) {
        addData(name, isFlag, isLeft, hasSetScale, false);
    }

    public void addData(String name, boolean isFlag, int isLeft, float hasSetScale, boolean isHindBitmap) {
        if (isFlag) {
            startX = X3ScreenUtils.getScreenWidth(mContext) / 2;
            startY = getHeight() / 2;
            d52 = 52;
            offSet = 60;
            currentScale = 1.0f;
            SCALE_MAX = 35.5f;
        }

        if (!X3StringUtils.isEmpty(name)) {
            TagLocation tagLocation = new TagLocation(startX * 1f / getWidth(), startY * 1f / getHeight());

            addItem(startX, startY, name, isLeft, tagLocation, hasSetScale, isHindBitmap);

            if (noticeView != null) {
                removeView(noticeView);
            }
            noticeView = new ImageView(mContext);
            LayoutParams layoutParams = new LayoutParams(X3ScreenUtils.dipToPx(134, mContext), X3ScreenUtils.dipToPx(35, mContext));
            layoutParams.topMargin = startY - X3ScreenUtils.dipToPx(5, mContext) - layoutParams.height;
            layoutParams.leftMargin = startX - layoutParams.width / 2;

            if (layoutParams.topMargin < 0) {
                layoutParams.topMargin = 0;
            }
            addView(noticeView, layoutParams);
        }
    }

    public void showTagLocation(Multimap<String, TagLocation> locationHashMap) {
        hashMap.clear();
        if (locationHashMap == null || locationHashMap.size() == 0) {
            return;
        }
        Multiset<String> keys = locationHashMap.keys();

        for (String key : keys.elementSet()) {
            Object[] tagLocations = locationHashMap.get(key).toArray();
            if (tagLocations == null) {
                continue;
            }
            for (int i = 0; i < tagLocations.length; i++) {
                TagLocation tagLocation = (TagLocation) tagLocations[i];
                startX = (int) (tagLocation.getX() * getWidth() / 1f);
                startY = (int) (tagLocation.getY() * getHeight() / 1f);
                int isLeft = (int) tagLocation.getIsLeft();
                addData(key, false, isLeft, tagLocation.scale, true);
            }
        }
    }

    public Multimap<String, TagLocation> getTagLocation() {
        if (hashMap == null) {
            return ArrayListMultimap.create();
        }
        return hashMap;
    }

    /**
     * 添加标签
     *
     * @param x
     * @param y
     * @param share
     */
    private void addItem(int x, int y, String share, int isLeft, TagLocation tagLocation) {
        addItem(x, y, share, isLeft, tagLocation, 1.0f);
    }

    /**
     * 添加标签
     *
     * @param x
     * @param y
     * @param share
     */
    private void addItem(int x, int y, String share, int isLeft, TagLocation tagLocation, float hasSetScale) {
        addItem(x, y, share, isLeft, tagLocation, hasSetScale, false);
    }

    /**
     * 添加标签
     *
     * @param x
     * @param y
     * @param share
     */
    private void addItem(int x, int y, String share, int isLeft, TagLocation tagLocation, float hasSetScale, boolean isHindBitmap) {

        View view = null;

        // 测量文字宽度 确定view的宽度
        textRect = new Rect();
        int size = 14;
        size = (int) (size * hasSetScale);
        textPaint.setTextSize(X3ScreenUtils.dipToPx(size, mContext));
        textPaint.getTextBounds(share, 0, share.length(), textRect);

        X3TagImageView.Direction direction = X3TagImageView.Direction.Left;

        int th = X3ScreenUtils.dipToPx(32, mContext);
        int tw = X3ScreenUtils.dipToPx(52, mContext);
        viewWidth = (int) (tw * hasSetScale) + X3ScreenUtils.dipToPx(offSetX, mContext);
        viewHeight = (int) (th * hasSetScale) + X3ScreenUtils.dipToPx(offSetY, mContext);

        if (hasSetScale >= 1){
            d52 = (int) (offSet * (hasSetScale-1) / 2) + offSet;
        }

        if (hasSetScale > 2.7){
            d52 = (int) (50 * (hasSetScale-1) / 2) + 50;
        }

        LayoutParams params = new LayoutParams(textRect.width() + X3ScreenUtils.dipToPx(d52, mContext), viewHeight);

        switch (isLeft) {
            case 0:
                if (x > getWidth() * 0.5) {
                    //Right是指 点在右边
                    view = new X3TagImageView(getContext());
                    //标签在右面 点击位置 x-标签宽度   右边的标签并不是以圆点开始的  而是以左边的wei
                    params.leftMargin = x;
                    params.topMargin = y;
                    view.setLayoutParams(params);
                    direction = X3TagImageView.Direction.Right;

                    tagLocation.setIsLeft(2);

                } else {
                    view = new X3TagImageView(getContext());
                    params.leftMargin = x;
                    params.topMargin = y;
                    view.setLayoutParams(params);
                    direction = X3TagImageView.Direction.Left;

                    tagLocation.setIsLeft(1);
                }
                break;
            case 1:
                view = new X3TagImageView(getContext());
                params.leftMargin = x;
                params.topMargin = y;
                view.setLayoutParams(params);
                direction = X3TagImageView.Direction.Left;

                tagLocation.setIsLeft(1);
                break;
            case 2:
                view = new X3TagImageView(getContext());
                //标签在右面 点击位置 x-标签宽度   右边的标签并不是以圆点开始的  而是以左边的wei
                params.leftMargin = x;
                params.topMargin = y;
                view.setLayoutParams(params);
                direction = X3TagImageView.Direction.Right;

                tagLocation.setIsLeft(2);
                break;
        }

        hashMap.put(share, tagLocation);
        //上下位置在视图内
        if (params.topMargin <= 0) {
            params.topMargin = 0;
        }
        if ((params.topMargin + params.height) >= getHeight()) {
            params.topMargin = getHeight() - params.height;
        }
        if (params.leftMargin <= 0) {
            params.leftMargin = 0;
        }
        if (params.leftMargin >= (getWidth() - params.width)) {
            params.leftMargin = getWidth() - params.width;
        }
        view.setTag(tagLocation);
        this.addView(view, params);
        ((X3TagImageView) view).setHindBitmap(isHindBitmap);
        ((X3TagImageView) view).refreshTag(hasSetScale);
        ((X3TagImageView) view).changeTag(share, direction);
    }

    int d52 = 52;
    int offSet = 60;

    private void refreshLayoutParams(float scale) {
        if (touchView != null) {

            if (scale > 1){
                d52 = (int) (offSet * (scale-1) / 2) + offSet;
            }

            if (scale >= 2.7){
                d52 = (int) (50 * (scale-1) / 2) + 50;
            }

            int size = 14;
            size = (int) (size * scale);
            textRect = new Rect();
            textPaint.setTextSize(X3ScreenUtils.dipToPx(size, mContext));

            String name = ((X3TagImageView) touchView).getShare();
            if (!X3StringUtils.isEmpty(name)) {
                textPaint.getTextBounds(name, 0, name.length(), textRect);
            }

            int th = X3ScreenUtils.dipToPx(32, mContext);
            int tw = X3ScreenUtils.dipToPx(52, mContext);

            viewWidth = (int) (tw * scale) + X3ScreenUtils.dipToPx(offSetX, mContext);
            viewHeight = (int) (th * scale) + X3ScreenUtils.dipToPx(offSetY, mContext);

            LayoutParams params = (LayoutParams) touchView.getLayoutParams();

            int w = textRect.width() + X3ScreenUtils.dipToPx(d52, mContext);
            if (w >= getWidth()){
                 w = getWidth();
                SCALE_MAX = scale;
            }

            params.width = w;
            params.height = viewHeight;

            if (params.topMargin <= 0) {
                params.topMargin = 0;
            }
            if ((params.topMargin + params.height) >= getHeight()) {
                params.topMargin = getHeight() - params.height;
            }

            if (params.leftMargin >= (getWidth() - params.width)) {
                params.leftMargin = getWidth() - params.width;
            }

            if (params.leftMargin <= 0) {
                params.leftMargin = 0;
            }
            touchView.setLayoutParams(params);
        }

    }

    /**
     * 判断此点击位置 是否已经有tag
     *
     * @param x
     * @param y
     * @return
     */
    private boolean hasView(int x, int y) {
        //循环获取子view，判断xy是否在子view上，即判断是否按住了子view
        for (int index = this.getChildCount() - 1; index >= 0; index--) {
            View view = this.getChildAt(index);
            if (view instanceof X3TagImageView) {
                int left = (int) view.getX();
                int top = (int) view.getY();
                int right = view.getRight();
                int bottom = view.getBottom();
                ((X3TagImageView) view).getShare();
                Rect rect = new Rect(left, top, right, bottom);

                boolean contains = rect.contains(x, y);
                int left2 = 0;
                int top2 = 0;
                int right2 = 0;
                Rect rect2 = null;
                if (((X3TagImageView) view).direction == X3TagImageView.Direction.Left) {
                    left2 = (int) (right - X3ScreenUtils.dipToPx(25,getContext()) );
                    top2 = (int) (top + X3ScreenUtils.dipToPx(25,getContext()) * currentScale);
                    rect2 = new Rect(left2, top2, right, bottom);
                } else {
                    right2 = (int) (left + X3ScreenUtils.dipToPx(25,getContext()) );
                    top2 = (int) (top + X3ScreenUtils.dipToPx(25,getContext()) * currentScale);
                    rect2 = new Rect(left, top2, right2, bottom);
                }
                if (rect2.contains(x, y)) {
                    isScaleIconTouch = true;
                } else {
                    isScaleIconTouch = false;
                }
                //如果是与子view重叠则返回真,表示已经有了view不需要添加新view了
                if (contains) {
                    touchView = view;
                    touchView.bringToFront();
                    return true;
                }
            }

        }
        touchView = null;
        return false;
    }

    private void  hindAllView() {
        for (int index = this.getChildCount() - 1; index >= 0; index--) {
            View view = this.getChildAt(index);
            if (view instanceof X3TagImageView) {
                ((X3TagImageView) view).setHindBitmap(true);
                ((X3TagImageView) view).refreshTag();
            }
        }
    }

    boolean changeOrientation ;
    /**
     * 移动tag
     *
     * @param x
     * @param y
     */
    private void moveView(int x, int y) {
        if (touchView == null || changeOrientation) {
            return;
        }

        TagLocation tmpTag = (TagLocation) touchView.getTag();

        String share = ((X3TagImageView) touchView).getShare();

        int th = X3ScreenUtils.dipToPx(32, mContext);
        int tw = X3ScreenUtils.dipToPx(52, mContext);

        int viewWidth = (int) (tw * tmpTag.scale) + X3ScreenUtils.dipToPx(offSetX, mContext);
        int viewHeight = (int) (th * tmpTag.scale) + X3ScreenUtils.dipToPx(offSetY, mContext);

        int size = 14;
        size = (int) (size * tmpTag.scale);
        textPaint.setTextSize(X3ScreenUtils.dipToPx(size, mContext));
        if (!X3StringUtils.isEmpty(share)) {
            textPaint.getTextBounds(share, 0, share.length(), textRect);
        }
        textPaint.getTextBounds(share, 0, share.length(), textRect);

        if (tmpTag.scale > 1){
            d52 = (int) (offSet * (tmpTag.scale-1) / 2) + offSet;
        }

        if (tmpTag.scale >= 2.7){
            d52 = (int) (50 * (tmpTag.scale-1) / 2) + 50;
        }

        LayoutParams params = new LayoutParams(textRect.width() + + X3ScreenUtils.dipToPx(d52, mContext), viewHeight);

        params.leftMargin = x - startX + startTouchViewLeft;
        params.topMargin = y - startY + startTouchViewTop;
        if (params.leftMargin < -X3ScreenUtils.dipToPx(20,getContext())){
            params.leftMargin = 0;
            changeOrientation = true;
            switchOrientation();
        }

        if ((params.leftMargin+textRect.width() + X3ScreenUtils.dipToPx(d52, mContext))>=getWidth() + X3ScreenUtils.dipToPx(20, mContext)){
            params.leftMargin = getWidth() - touchView.getWidth();
            changeOrientation = true;
            switchOrientation();
        }

        //限制子控件移动必须在视图范围内
        if (params.leftMargin < 0) {
//            params.leftMargin = 0;
        }
        if ((params.leftMargin + touchView.getWidth()) > getWidth()) {
//            params.leftMargin = getWidth() - touchView.getWidth();
        }
        if (params.topMargin < 0) {
            params.topMargin = 0;
        }
        if ((params.topMargin + params.height) >= getHeight()) {
            params.topMargin = getHeight() - params.height;
        }

        touchView.setLayoutParams(params);

        TagLocation tag = (TagLocation) touchView.getTag();

        hashMap.remove(share, touchView.getTag());
        TagLocation tagLocation = new TagLocation(params.leftMargin * 1f / getWidth(), params.topMargin * 1f / getHeight(), tag.getIsLeft());
        if (tmpTag != null) {
            tagLocation.scale = tmpTag.scale;
        }

        hashMap.put(share, tagLocation);
        touchView.setTag(tagLocation);

        if (onTouchLayoutListence != null) {
            onTouchLayoutListence.OnChangeTagLocation();
        }
    }

    public void removeViews() {
        this.removeAllViews();
        addView(delView, deleteImageLayout);
        delView.setAlpha(0f);
    }

    public interface OnTouchLayoutListence {
        void OnTouchListener();

        // 是否显示底部的按钮 裁剪 标签 旋转
        void OnShowBottomView(boolean isShow);

        /**
         * 滑动位置更新对应坐标
         */
        void OnChangeTagLocation();
    }

    public void setOnTouchLayoutListence(OnTouchLayoutListence onTouchLayoutListence) {
        this.onTouchLayoutListence = onTouchLayoutListence;
    }
}
